﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;

    public class ReceiptRevert : BasePage
    {
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected HtmlForm F4031;
        protected GridView gvadjustmentDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnReceiptID;
        protected HtmlHead Head1;
        protected HtmlInputHidden hidCheck;
        protected HtmlImage img;
        protected HtmlImage img1;
        protected Label lblAdJustMentDate;
        protected Label lblAdJustMentID;
        protected Label lblASN;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblReceiptID;
        protected Label lblTotal;
        protected Label lblTotalQty;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        private decimal TotalQty;
        protected TextBox txtAdJustMentDate;
        protected TextBox txtAdJustMentID;
        protected TextBox txtOwnerID;
        protected TextBox txtReceiptID;
        protected UpdatePanel up1;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ReceiptRevert.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ReceiptRevertList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.txtAdJustMentID.Text = string.Empty;
            this.txtAdJustMentDate.Text = string.Empty;
            this.txtOwnerID.Text = string.Empty;
            this.txtReceiptID.Text = string.Empty;
            this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.hdnFlag.Value = "0";
            this.hdnReceiptID.Value = "";
            this.btnSave.Enabled = true;
            this.img.Disabled = false;
            this.img1.Disabled = false;
            base.BindData2GridView(this.gvadjustmentDetail, new List<AdjustmentDetailInfo>());
            this.txtEnabled();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            if (this.btnSave.Enabled)
            {
                if (!string.IsNullOrEmpty(this.txtReceiptID.Text))
                {
                    this.ReceiptID_TextChanged(null, null);
                }
            }
            else
            {
                this.hdnReceiptID.Value = this.txtAdJustMentID.Text;
                this.SetAdJustMentInfo2Form();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtReceiptID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4300_001");
                this.lblMsg.Visible = true;
            }
            else
            {
                base.SaveSelectedInGridView(this.gvadjustmentDetail, "TransID");
                List<string> selectedID = base.DataGridViewState as List<string>;
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4300_002");
                    this.lblMsg.Visible = true;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
                else if (string.IsNullOrEmpty(this.txtAdJustMentDate.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4300_003");
                    this.lblMsg.Visible = true;
                }
                else
                {
                    try
                    {
                        int i;
                        AdjustmentInfo adjument = base.DataEntityViewState as AdjustmentInfo;
                        adjument.IsActive = "Y";
                        if (!string.IsNullOrEmpty(this.txtAdJustMentDate.Text))
                        {
                            adjument.AdjustmentDate = Convert.ToDateTime(this.txtAdJustMentDate.Text);
                        }
                        adjument.OwnerID = this.txtOwnerID.Text;
                        adjument.CreatedBy = base.UserInfo.UserID;
                        adjument.UpdatedBy = base.UserInfo.UserID;
                        List<AdjustmentDetailInfo> adjumentDetailList = new List<AdjustmentDetailInfo>();
                        List<string> StrLineID = new List<string>();
                        foreach (AdjustmentDetailInfo info in adjument.AdjustmentDetail)
                        {
                            i = 0;
                            while (i < selectedID.Count)
                            {
                                if (selectedID[i] == info.TransID.ToString())
                                {
                                    info.AdjustmentDate = adjument.AdjustmentDate;
                                    if (info.IsGenPutAway == "N")
                                    {
                                        if (!StrLineID.Contains(info.ReceiptLineID.ToString()))
                                        {
                                            StrLineID.Add(info.ReceiptLineID.ToString());
                                        }
                                        adjumentDetailList.Add(info);
                                    }
                                    else
                                    {
                                        this.lblMsg.Text = base.GetGlobalMsgResource("F4300_004");
                                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                                        return;
                                    }
                                }
                                i++;
                            }
                        }
                        adjument.AdjustmentDetail = adjumentDetailList;
                        string strAdjumentID = string.Empty;
                        strAdjumentID = InboundServiceClientProxy.GetInstance().ReceiptRevert(adjument);
                        if (!string.IsNullOrEmpty(strAdjumentID))
                        {
                            string lineID = string.Empty;
                            for (i = 0; i < StrLineID.Count; i++)
                            {
                                lineID = lineID + StrLineID[i] + ",";
                            }
                            base.DataGridViewState = null;
                            this.SetAdJustMentInfo2Form();
                            lineID = (lineID.Length > 0) ? lineID.Substring(0, lineID.Length - 1) : "";
                            this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F4300_005"), this.txtReceiptID.Text, lineID);
                            this.lblMsg.Visible = true;
                            this.txtAdJustMentID.Text = strAdjumentID;
                            this.btnSave.Enabled = false;
                            this.SetAdJustMentInfo2Form();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = ex.Message;
                        this.lblMsg.Visible = true;
                    }
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
            }
        }

        protected void gvadjustmentDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F4031", this.gvadjustmentDetail);
        }

        protected void gvadjustmentDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            base.SaveSelectedInGridView(this.gvadjustmentDetail, "ReceiptLineID");
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as AdjustmentInfo).AdjustmentDetail;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvadjustmentDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvadjustmentDetail.PageIndex * this.gvadjustmentDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewCheckInDataGridViewState(this.gvadjustmentDetail.DataKeys[e.Row.RowIndex]["ReceiptLineID"].ToString(), (CheckBox) e.Row.FindControl("chbSelect"));
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.TotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQty.Text = base.GetPreferenceDigit(this.TotalQty);
            }
        }

        protected void gvadjustmentDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                AdjustmentInfo adjustmentInfo = base.DataEntityViewState as AdjustmentInfo;
                DataView dv = CADataConverter.ToDataTable(adjustmentInfo.AdjustmentDetail).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvadjustmentDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void LoadData()
        {
            this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReceiptID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetAdJustMentInfo2Form();
                this.img.Disabled = true;
                this.img1.Disabled = true;
                this.btnSave.Enabled = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                this.txtEnabled();
            }
        }

        protected void ReceiptID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtReceiptID.Text))
                {
                    ReceiptInfo receipt = InboundServiceClientProxy.GetInstance().GetReceiptInfoByID4Exec(this.txtReceiptID.Text.Trim());
                    if ((receipt != null) && !string.IsNullOrEmpty(receipt.ReceiptID))
                    {
                        this.txtOwnerID.Text = receipt.OwnerID;
                        this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(DateTime.Now);
                        AdjustmentInfo adjustmentinfoList = new AdjustmentInfo {
                            AdjustmentDetail = new List<AdjustmentDetailInfo>()
                        };
                        int LineID = 0;
                        foreach (ReceiptHistoryInfo receiptinfo in receipt.ReceiptHistoryInfos)
                        {
                            LineID += 10;
                            if (receiptinfo.Status != "New")
                            {
                                AdjustmentDetailInfo adjustmentinfo = new AdjustmentDetailInfo {
                                    AdjustmentID = this.txtAdJustMentID.Text
                                };
                                if (!string.IsNullOrEmpty(this.txtAdJustMentDate.Text))
                                {
                                    adjustmentinfo.AdjustmentDate = Convert.ToDateTime(this.txtAdJustMentDate.Text);
                                }
                                adjustmentinfo.CaseCnt = 0M;
                                adjustmentinfo.Cube = 0.0;
                                adjustmentinfo.ExtensionData = receiptinfo.ExtensionData;
                                adjustmentinfo.GrossWgt = 0.0;
                                adjustmentinfo.InnerPack = 0M;
                                adjustmentinfo.LineID = LineID;
                                adjustmentinfo.Loc = receiptinfo.Loc;
                                adjustmentinfo.Lot = receiptinfo.LotID;
                                adjustmentinfo.LpnID = receiptinfo.Lpn;
                                adjustmentinfo.NetWgt = 0.0;
                                adjustmentinfo.OtherUnit1 = 0.0;
                                adjustmentinfo.OtherUnit2 = 0.0;
                                adjustmentinfo.OwnerID = receiptinfo.OwnerID;
                                adjustmentinfo.PackID = receiptinfo.PackID;
                                adjustmentinfo.Pallet = 0M;
                                adjustmentinfo.Qty = receiptinfo.ReceivedQty * -1M;
                                adjustmentinfo.ReasonCode = "UnReceive";
                                adjustmentinfo.ReceiptID = receiptinfo.ReceiptID;
                                adjustmentinfo.ReceiptLineID = int.Parse(receiptinfo.ReceiptLineID);
                                adjustmentinfo.Remark = "";
                                adjustmentinfo.SkuID = receiptinfo.SkuID;
                                adjustmentinfo.UomID = receiptinfo.Uom;
                                adjustmentinfo.WhID = "";
                                adjustmentinfo.CreatedBy = base.UserInfo.UserID;
                                adjustmentinfo.CreatedDate = DateTime.Now;
                                adjustmentinfo.IsGenPutAway = receiptinfo.IsGenPutAway;
                                adjustmentinfoList.AdjustmentDetail.Add(adjustmentinfo);
                                adjustmentinfo.TransID = receiptinfo.TransID;
                            }
                        }
                        if (adjustmentinfoList.AdjustmentDetail.Count > 0)
                        {
                            base.DataEntityViewState = adjustmentinfoList;
                            base.BindData2GridView(this.gvadjustmentDetail, adjustmentinfoList.AdjustmentDetail);
                            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F4300_007");
                        }
                    }
                    else
                    {
                        this.txtReceiptID.Text = "";
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4300_008");
                        this.lblMsg.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void SetAdJustMentInfo2Form()
        {
            try
            {
                string adjustmentID = this.hdnReceiptID.Value.ToString();
                AdjustmentInfo adjustmentInfo = InboundServiceClientProxy.GetInstance().GetAdjustmentInfoByID(adjustmentID);
                base.DataEntityViewState = adjustmentInfo;
                if (adjustmentInfo != null)
                {
                    this.txtAdJustMentID.Text = adjustmentInfo.AdjustmentID;
                    this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(adjustmentInfo.AdjustmentDate);
                    this.txtOwnerID.Text = adjustmentInfo.OwnerID;
                    if ((adjustmentInfo.AdjustmentDetail != null) && (adjustmentInfo.AdjustmentDetail.Count > 0))
                    {
                        this.txtReceiptID.Text = adjustmentInfo.AdjustmentDetail[0].ReceiptID;
                        int LineID = adjustmentInfo.AdjustmentDetail[adjustmentInfo.AdjustmentDetail.Count - 1].LineID + 10;
                    }
                    base.BindData2GridView(this.gvadjustmentDetail, adjustmentInfo.AdjustmentDetail);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void txtEnabled()
        {
            string CS40000 = this.hdnFlag.Value;
            if (CS40000 != null)
            {
                if (!(CS40000 == "") && !(CS40000 == "0"))
                {
                    if (CS40000 == "1")
                    {
                        this.txtAdJustMentID.Enabled = false;
                        this.txtAdJustMentDate.Enabled = false;
                        this.txtOwnerID.Enabled = false;
                        this.txtReceiptID.Enabled = false;
                    }
                }
                else
                {
                    this.txtAdJustMentID.Text = base.GetGlobalMsgResource("F4300_006");
                    this.txtAdJustMentID.Enabled = false;
                    this.txtAdJustMentDate.Enabled = true;
                    this.txtOwnerID.Enabled = false;
                    this.txtReceiptID.Enabled = true;
                }
            }
        }
    }
}

